#include "Menu.h"
#include "STDIO.H"
// 函数声明
void HiBoard();
void HomePage();

void Menu_Init() {
    NextMode = 1; // 初始化进入首页
}

/**
 * @brief 负一屏
 */
void HiBoard() {
    OLED_ShowString(8, 0, "Producer:HJei", 16);
    OLED_ShowString(32, 2, "HLan;CAo", 16);
    OLED_ShowString(16, 4, "XYang;LZheng", 16);
    OLED_ShowString(104, 6, "-->", 16);
}

/**
 * @brief 起始页
 */
void HomePage() {
    OLED_ShowString(24, 0, "Welcome To", 16);
    OLED_ShowString(16, 2, "Appreciation", 16);
    OLED_ShowString(0, 4, "NiuPi Trash Can", 16);
    OLED_ShowString(0, 6, "<--", 16);
    OLED_ShowString(104, 6, "-->", 16);
}

/**
 * @brief 菜单显示
 * @param Option 菜单选项结构体数组
 * @param a 起始选项从0开始 如0-4 就显示0 1 2 3
 * @param b 结束选项
 */
static void MenuShow(Option_Class *Option,char a,char b){
    char i;
    for (i=0; a < b; a++,i++) {
        OLED_ShowString(0, 2*i, Option[a].OName, 16);
    }
}

//数字限幅，以及判断确认 返回
char Digital_Clipping(char start,char over,char span,char *target){
    if(EntityKey_Check(1,KEY_CLICK)){
        *target-=span;
        if(*target<=start-1){*target=over;}
        OLED_Clear();
    }else if(EntityKey_Check(2,KEY_CLICK)){
        *target+=span;
        if(*target>=over+1){*target=start;}
        OLED_Clear();
    }else if(EntityKey_Check(3,KEY_CLICK)){
        OLED_Clear();
        return 0x03;
    }else if(EntityKey_Check(4,KEY_CLICK)){
        OLED_Clear();
        return 0x04;
    }
    return 0;
}




//一级菜单
static char menu1(){
    char flag=1;//这个地方有个BUG，当flag=1时，第一次进入多级菜单光标会在第二行
    char temp;
    Option_Class Option[8]={
        {"1:lin"},
        {"2:Data"},
        {"3:Time"},
        {"4:Sum"},
        {"5:Temp"},
        {"6:Humt"},
        {"7:State"},
        {"8:operate"}
    };
    EntityKey_Clear();
    OLED_Clear();
    while(1){
        temp=Digital_Clipping(1,8,1,&flag);
        if(temp==0x03){return flag;}
        else if(temp==0x04){return 0;}//返回上一级菜单
        if(flag<=4){MenuShow(Option,0,4);}
        else{MenuShow(Option,4,8);}
        OLED_ShowString(88, 2*((flag-1)%4), "<<<<<", 16);
    }
}

static char span=0;
static char angle=0;
static char speed=0;
//一级菜单的第1个 二级菜单的第1个的三级菜单
static char menu3_21(){
    while (1){
        if(Digital_Clipping(1,30,1,&span))
            return 0;
        OLED_ShowString(0, 0, "<--", 16);
        OLED_ShowString(0, 2, "span:", 16);
        OLED_ShowNum(40, 2, span, 2, 16);
        OLED_ShowString(0, 4, "angle:", 16);
        OLED_ShowNum(48, 4, angle, 3, 16);
        OLED_ShowString(0, 6, "speed:", 16);
        OLED_ShowNum(48, 6, speed, 2, 16);
        OLED_ShowString(72, 2, "<------", 16);
    }
    
}
//一级菜单第1行的二级菜单中的第3行的(三级菜单)
static char menu3_31(){
    while (1){
        if(Digital_Clipping(1,120,span,&angle))
            return 0;
        OLED_ShowString(0, 0, "<--", 16);
        OLED_ShowString(0, 2, "span:", 16);
        OLED_ShowNum(40, 2, span, 2, 16);
        OLED_ShowString(0, 4, "angle:", 16);
        OLED_ShowNum(48, 4, angle, 3, 16);
        OLED_ShowString(0, 6, "speed:", 16);
        OLED_ShowNum(48, 6, speed, 2, 16);
        OLED_ShowString(80, 4, "<-----", 16);
    }
}

//一级菜单第1行的二级菜单中的第4行的(三级菜单)
static char menu3_41(){
    while (1){
        OLED_ShowString(0, 0, "<--", 16);
        OLED_ShowString(0, 2, "span:", 16);
        OLED_ShowNum(40, 2, span, 2, 16);
        OLED_ShowString(0, 4, "angle:", 16);
        OLED_ShowNum(48, 4, angle, 3, 16);
        OLED_ShowString(0, 6, "speed:", 16);
        OLED_ShowNum(48, 6, speed, 2, 16);
        OLED_ShowString(80, 6, "<-----", 16);
        if(EntityKey_Check(2,KEY_CLICK)){
            speed++;
        }else if(EntityKey_Check(2,KEY_DOUBLE)){
            speed+=2;
        }else if(EntityKey_Check(2,KEY_LONG)){
            speed+=10;
        }else if(EntityKey_Check(3,KEY_CLICK)||EntityKey_Check(4,KEY_CLICK)){
            return 0;
        }
    }
}

//一级菜单第8行的二级菜单中的第3行的(三级菜单)
static char menu3_38(){
    Option_Class Option[4]={
        {"<--"},
        {"Can Switch:"},
    };
    while (1){
        MenuShow(Option,0,2);
        if(Digital_Clipping(0,1,1,&BinSw)){
            if(BinSw==0){
                Engine_SetAngle(0);
                Motor_SetSpeed(0);//BUG,不知道为啥会带着马达震动
            }else if(BinSw==1){
                Engine_SetAngle(180);
                Motor_SetSpeed(0);//BUG,不知道为啥会带着马达震动
            }
            return 0;
        }
        if(BinSw){
            OLED_ShowString(0,4,"    ->OPEN<-     ",16);
            os_wait2(K_TMO,2);
            OLED_ShowString(0,4,"   -->OPEN<--    ",16);
            os_wait2(K_TMO,2);
            OLED_ShowString(0,4,"  --->OPEN<---   ",16);
            os_wait2(K_TMO,2);
        }else{
            OLED_ShowString(0,4,"    ->CLOSE<-    ",16);
            os_wait2(K_TMO,2);
            OLED_ShowString(0,4,"   -->CLOSE<--   ",16);
            os_wait2(K_TMO,2);
            OLED_ShowString(0,4,"  --->CLOSE<---  ",16);
            os_wait2(K_TMO,2);
        }
        
    }
    return 0;
}




//一级菜单第1行的二级菜单
static char menu2_1(){
    char m2flag=1;
    char menu3=0;//三级菜单变量
    char temp=0;
    Option_Class Option[4]={
        {"<--"},
        {"span:"},
        {"angle:"},
        {"speed:"},
    };
    EntityKey_Clear();
    while(1){
        MenuShow(Option,0,4);
        OLED_ShowString(88, 2*(m2flag-1), "<<<<<", 16);
        temp=Digital_Clipping(1,4,1,&m2flag);

        if(temp==0x03){menu3=m2flag;}
        else if(temp==0x04){return 0;}//返回上一级菜单

        //--------------二级菜单中检测是否进入三级菜单
        if (menu3 == 1) { // 第一行退出，返回一级菜单
            return 0;
        }
        else if (menu3 == 2) {//一级菜单第1行的二级菜单中的第2行的(三级菜单)
            menu3 = menu3_21();
            OLED_Clear();
        }else if (menu3 == 3) {//一级菜单第1行的二级菜单中的第3行的(三级菜单)
            menu3 = menu3_31();
            OLED_Clear();
        }else if (menu3 == 4) {//一级菜单第1行的二级菜单中的第4行的(三级菜单)
            menu3 = menu3_41();
            OLED_Clear();
        }
    }
}
//一级菜单第二行的二级菜单
static char menu2_2(){
    Clock_t clock;
    Option_Class Option[4]={
        {"<--"},
        {"Year:"},
        {"Month:"},
        {"Day:"},
    };
    EntityKey_Clear();
    MenuShow(Option,0,4);
    PCF8563_Get_Clock(&clock);
    OLED_ShowNum(40,2,clock.year,4,16);
    OLED_ShowNum(48,4,clock.month,2,16);
    OLED_ShowNum(32,6,clock.day,2,16);
    OLED_ShowString(88, 0, "<<<<<", 16);
    while (1){
        if(EntityKey_Check(3,KEY_CLICK)||EntityKey_Check(4,KEY_CLICK))
            return 0;
    }
}

//一级菜单第3行的二级菜单
static char menu2_3(){
    Clock_t clock;
    Option_Class Option[4]={
        {"<--"},
        {"Hour:"},
        {"minute:"},
        {"second:"},
    };
    EntityKey_Clear();
    MenuShow(Option,0,4);
    OLED_ShowString(88, 0, "<<<<<", 16);
    while (1){
        PCF8563_Get_Clock(&clock);
        OLED_ShowNum(40,2,clock.hour,2,16);
        OLED_ShowNum(56,4,clock.minute,2,16);
        OLED_ShowNum(56,6,clock.second,2,16);
        if(EntityKey_Check(3,KEY_CLICK)||EntityKey_Check(4,KEY_CLICK))
            return 0;
    }
}

//一级菜单第5行的二级菜单
static char menu2_5(){
    int humidity;
    float temperature;
    EntityKey_Clear();
    DHT_Get_Humidity_Temperature(&humidity, &temperature);
    while(1) {
        OLED_ShowString(0, 0, "<--", 16);
        OLED_ShowString(88, 0, "<<<<<", 16);
        OLED_ShowString(0, 2, "Temperature:", 16);
        OLED_ShowNum(84, 4, temperature, 2, 16);
        OLED_ShowString(108, 4, "C", 16);
        if(EntityKey_Check(3,KEY_CLICK)||EntityKey_Check(4,KEY_CLICK))
            return 0;
    }
}

static char menu2_6(){
    int humidity;
    float temperature;
    EntityKey_Clear();
    DHT_Get_Humidity_Temperature(&humidity, &temperature);
    while(1) {
        OLED_ShowString(0, 0, "<--", 16);
        OLED_ShowString(88, 0, "<<<<<", 16);
        OLED_ShowString(0, 2, "Humidity:", 16);
        OLED_ShowNum(72, 4, humidity, 2, 16);
        OLED_ShowString(96, 4, "%", 16);
        if(EntityKey_Check(3,KEY_CLICK)||EntityKey_Check(4,KEY_CLICK))
            return 0;
    }
}
static char menu2_7(){
    char state;
    RedRay_Init();
    EntityKey_Clear();
    while(1) {
        state=RedRay_State();
        OLED_ShowString(0, 0, "<--", 16);
        OLED_ShowString(88, 0, "<<<<<", 16);
        OLED_ShowString(0, 2, "State:", 16);
        if(state==1){
            OLED_ShowString(0, 4, "Full     ", 16);
        }else{
            OLED_ShowString(0, 4, "Not Full", 16);
        }
        if(EntityKey_Check(3,KEY_CLICK)||EntityKey_Check(4,KEY_CLICK))
            return 0;
    }
}

static char menu2_8(){
    char menu3=0;//三级指针变量
    char m2flag=1;//光标位置变量
    char temp=0;
    Option_Class Option[4]={
        {"<--"},
        {"Can Switch:"},
    };
    EntityKey_Clear();
    while(1) {
        MenuShow(Option,0,2);
        if(BinSw){
            OLED_ShowString(24,4,"OPEN",16);
        }else{
            OLED_ShowString(24,4,"CLOSE",16);
        }
        OLED_ShowString(88,2*(m2flag-1),"<<<<<",16);
        temp=Digital_Clipping(1,3,1,&m2flag);
        if(temp==0x03){menu3=m2flag;}
        else if(temp==0x04){return 0;}
        //--------------------------------------------------
        if(menu3==1){//第一行退出，返回一级菜单
            return 0;
        }else if(menu3==3){//一级菜单第8行的二级菜单中的第三行的（三级菜单）
            menu3=menu3_38();
            OLED_Clear();
        }
    }
}


void Multi_Menu() {
    char menu2=0;
    OLED_Clear();
    while (1) {
        menu2=menu1();
        if(menu2==0){
            return;
        }else if(menu2==1){//进入1级菜单的第1行的(二级菜单)
            menu2_1();
        }else if(menu2==2){//进入1级菜单的第2行的(二级菜单)
            menu2_2();
        }else if(menu2==3){//进入1级菜单的第3行的(二级菜单)
            menu2_3();
        }else if(menu2==4){//进入1级菜单的第4行的(二级菜单)
            //sum总数 留空
        }else if(menu2==5){//进入1级菜单的第5行的(二级菜单)
            menu2_5();
        }else if(menu2==6){//进入1级菜单的第6行的(二级菜单)
            menu2_6();
        }else if(menu2==7){//进入1级菜单的第7行的(二级菜单)
            menu2_7();
        }else if(menu2==8){//进入1级菜单的第7行的(二级菜单)
            menu2_8();
        }
    }
}
